package com.wiscamp.ninechapters.panels.domain.entities;

import com.wiscamp.ninechapters.panels.domain.enums.GroupTypes;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import cube.ddd.domain.Entity;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;

/**
 * 用户组表
 */
@Data
public class Group implements Entity {

    /**
     * 用户组编号
     */
    private long groupId = 0L;

    /**
     * 应用编号
     */
    private long applicationId = 0L;

    /**
     * 用户组代码
     */
    private String groupCode = "";

    /**
     * 用户组名称
     */
    private String groupName = "";

    /**
     * 用户组类型编号
     */
    private long groupTypeId = 0L;

    /**
     * 备注
     */
    private String remark = "";

    /**
     * 创建用户
     */
    private long creatorId = 0L;

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime creationTime;

    /**
     * 编辑用户
     */
    private long modifierId = 0L;

    /**
     * 编辑时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime modificationTime;

    /**
     * 是否启用
     */
    private boolean enabled = true;

    /**
     * 是否删除
     */
    private boolean deleted = false;

    /**
     * 用户组类型名称
     */
    private String groupTypeName = "";

    /**
     * 创建人名称
     */
    private String creatorName = "";

    /**
     * 编辑人名称
     */
    private String modifierName = "";


    //region groupUsers
    /**
     * 用户列表
     */
    private Collection<Long> userIds = new ArrayList<>();
    //endregion

    //region groupUsers
    /**
     * 角色列表
     */
    private Collection<Long> roleIds = new ArrayList<>();
    //endregion

    public void enable() {
        if (!this.enabled) {
            this.enabled = true;
        }
    }

    public void disable() {
        if (this.enabled) {
            this.enabled = false;
        }
    }

    public void delete() {
        if (!this.deleted) {
            this.deleted = true;
        }
    }

    public void createBy(long operatorId) {
        this.creatorId = operatorId;
        this.creationTime = LocalDateTime.now();
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public void updateBy(long operatorId) {
        this.modifierId = operatorId;
        this.modificationTime = LocalDateTime.now();
    }

    public Group(long appId, String groupCode, String groupName, GroupTypes groupType) {
        this.setApplicationId(appId);
        this.setGroupCode(groupCode);
        this.setGroupName(groupName);
        if (Objects.nonNull(groupType)) {
            this.setGroupTypeId(groupType.getIndex());
        }
    }
}
